package fr.greta.centreFormation.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.greta.centreFormation.bean.Cursus;
import fr.greta.centreFormation.bean.Promotion;
import fr.greta.centreFormation.dao.Bdd;
import fr.greta.centreFormation.dao.CursusHome;
import fr.greta.centreFormation.dao.PromotionHome;

/**
 * Servlet implementation class ScriptModifPromotion
 */
public class ScriptModifPromotion extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ScriptModifPromotion() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		////----------------------------------\\\\
		///		V�rification des donn�es	   \\\
		//--------------------------------------\\

		String jourDebut = request.getParameter("jourDebut");
		String moisDebut = request.getParameter("moisDebut");
		String anneeDebut = request.getParameter("anneeDebut");
		String dateDebutString = anneeDebut + "-" + moisDebut + "-" + jourDebut;

		String jourFin = request.getParameter("jourFin");
		String moisFin = request.getParameter("moisFin");
		String anneeFin = request.getParameter("anneeFin");
		String dateFinString = anneeFin + "-" + moisFin + "-" + jourFin;

		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dateDebut = null;
		java.util.Date datefin = null;
		try {
			dateDebut = formatDate.parse(dateDebutString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			datefin = formatDate.parse(dateFinString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String idCursusString = request.getParameter("cursus");
		Integer idCursus = Integer.parseInt(idCursusString);

		HttpSession session = request.getSession(true);



		////----------------------------------\\\\
		///		Insertion dans la base		   \\\
		//--------------------------------------\\

		Bdd connection =  new Bdd();
		Promotion p = new Promotion();

		Cursus c = CursusHome.selectUnCursusById(idCursus);

		p.setIdPromotion((Integer) session.getAttribute("idPromotion"));
		p.setCursus(c);
		p.setDebutPromotion(dateDebut);
		p.setFinPromotion(datefin);

		System.out.println("resultat update => "+ PromotionHome.updatePromotionSansSesCandidats(p));

		response.sendRedirect("ModifPromotion");
		//request.getRequestDispatcher("ModifPromotion").forward(request, response);


	}

}
